package com.xiaoxue.xiaoxuesp.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static javax.servlet.http.HttpServletResponse.SC_SEE_OTHER;

@Controller
@RequestMapping(path = "redirect")
public class TestController {

    @ResponseBody
    @GetMapping(path = "index")
    public String firstController(HttpServletRequest request) {

        return "重定向访问! " + JSONObject.toJSONString(request.getParameterMap());
    }


    /**
     * 访问本接口重定向到index接口
     * @return
     */
    @GetMapping(path = "r1")
    public String r1() {
        return "redirect:/redirect/index?base=r1";
    }

    @ResponseBody
    @GetMapping(path = "r2")
    public void r2(HttpServletResponse response) throws IOException {
        response.setStatus(SC_SEE_OTHER);
        response.sendRedirect("/redirect/index?base=r2");
    }






}
